package com.cnd.scm.rabbitmq.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @ClassName: MessageSenderFactory
 * @Description:
 * @author: 李启联
 * @date: 2021-03-18 18:19
 * @Copyright: Copyright (c), 2021
 */
@Component
@Configuration
public class MessageSenderFactory {

    @Value("${fastRabbitmq.messageSenderTypeName}")
    private String messageSenderTypeName;

    public String getMessageSenderTypeName() {
        return messageSenderTypeName;
    }

    public void setMessageSenderTypeName(String messageSenderTypeName) {
        this.messageSenderTypeName = messageSenderTypeName;
    }

    Map<String,MessageSender> sendersMap = new HashMap<>();

    public MessageSenderFactory(List<MessageSender> senders) {
        for (MessageSender messageSender : senders) {
            sendersMap.put(messageSender.getClass().getTypeName(),messageSender);
        }
    }

    /**
     * 创建具体实现者
     * @return
     */
    public MessageSender createMessageSender() {
        MessageSender  messageSender = sendersMap.get(getMessageSenderTypeName());
        if(null == messageSender){
            messageSender = new SimpleMessageSender();
        }
        messageSender.retry();
        return messageSender;
    }

}